package com.ywk.miaohsa.controller;

import com.ywk.miaohsa.result.Result;
import com.ywk.miaohsa.service.MiaoshaUserService;
import com.ywk.miaohsa.vo.LoginVo;
import com.ywk.miaohsa.vo.MiaoshaUserVo;
import com.ywk.miaohsa.vo.UserInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Controller
@RequestMapping("/login")
public class LoginController {

    @Autowired
    MiaoshaUserService miaoshaUserService;

    @RequestMapping("/to_login")
    public String toLogin() {
        return "login";
    }

    @RequestMapping("/do_login")
    @ResponseBody//使用@ResponseBody 注解实现将 controller 方法返回对象转换为 json 响应给客户端。
    public Result<MiaoshaUserVo> doLogin(HttpServletResponse response, @Valid @RequestBody LoginVo loginVo){
        System.out.println(loginVo.getMobile());
        MiaoshaUserVo userVo=miaoshaUserService.login(response,loginVo);
        return Result.success(userVo);
    }

    @RequestMapping("/register")
    @ResponseBody//使用@ResponseBody 注解实现将 controller 方法返回对象转换为 json 响应给客户端。
    public Result<String> register(@Valid @RequestBody LoginVo loginVo){
        System.out.println(loginVo.getMobile());
        miaoshaUserService.register(loginVo);
        return Result.success("success");
    }
}
